Imports System.Windows.Forms

Public Class dlgUser

    Private Sub dlgUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cbUserID.DataSource = db.getvalue("select * from users").Tables(0)
        cbUserID.DisplayMember = "UserID"

    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim sql As String
        If db.getvalue("select * from users where userid = '" & cbUserID.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
            MsgBox("Duplicate!!!")
            Exit Sub
        End If
        If txtPassword.Text.Replace("'", "''") = "" Then
            MsgBox("The Password is not allowed!")
            Exit Sub
        End If
        If txtPassword.Text <> txtCpassword.Text Then
            MsgBox("Confirm Password is wrong!")
            Exit Sub
        End If
        sql = "insert into users(userid,uname,upassword,uadmin,uactive,uconfirm)" _
        & " values('" & cbUserID.Text.Replace("'", "''") _
        & "','" & txtFullName.Text.Replace("'", "''") _
        & "','" & Encode(txtPassword.Text.Replace("'", "''")) _
        & "'," & chkAdmin.Checked * -1 _
        & "," & chkActive.Checked * -1 _
        & "," & chkconfirm.Checked * -1 & ")"
        Try
            db.update(sql)
            MsgBox("the Userid " & cbUserID.Text & " has been created.")
            cbUserID.DataSource = db.getvalue("select * from users").Tables(0)
            cbUserID.DisplayMember = "UserID"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        


    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim sql As String
        If db.getvalue("select * from users where userid = '" & cbUserID.Text.Replace("'", "") & "'").Tables(0).Rows.Count = 0 Then
            MsgBox("The userid " & cbUserID.Text & " is not found!!!")
            Exit Sub
        End If
        If txtPassword.Text.Replace("'", "''") = "" Then
            MsgBox("The Password is not allowed!")
            Exit Sub
        End If
        If txtPassword.Text <> txtCpassword.Text Then
            MsgBox("Confirm Password is wrong!")
            Exit Sub
        End If
        sql = "update users set " _
       & " uName='" & txtFullName.Text.Replace("'", "''") _
       & "',uPassword='" & Encode(txtPassword.Text.Replace("'", "''")) _
       & "',uadmin=" & chkAdmin.Checked * -1 _
       & ",uactive=" & chkActive.Checked * -1 _
        & ",uconfirm=" & chkconfirm.Checked * -1 _
       & " where userid = '" & cbUserID.Text.Replace("'", "''") & "'"
        Try
            db.update(sql)
            MsgBox("the Userid " & cbUserID.Text & " has been updated.")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        

    End Sub

    Private Sub cbUserID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbUserID.SelectedIndexChanged
        Dim ds As DataSet
        ds = db.getvalue("select * from users where userid='" & cbUserID.Text.Replace("'", "''") & "'")
        If ds.Tables(0).Rows.Count > 0 Then
            txtFullName.Text = ds.Tables(0).Rows(0).Item("uname")
            txtPassword.Text = Decode(ds.Tables(0).Rows(0).Item("upassword"))
            txtCpassword.Text = txtPassword.Text
            chkAdmin.Checked = IIf(IsDBNull(ds.Tables(0).Rows(0).Item("uadmin")), False, ds.Tables(0).Rows(0).Item("uadmin"))
            chkActive.Checked = IIf(IsDBNull(ds.Tables(0).Rows(0).Item("uactive")), False, ds.Tables(0).Rows(0).Item("uactive"))
            chkconfirm.Checked = IIf(IsDBNull(ds.Tables(0).Rows(0).Item("uconfirm")), False, ds.Tables(0).Rows(0).Item("uconfirm"))
        End If
    End Sub


End Class
